﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Windows;
using System.Windows.Documents;

namespace DetectApp;

/// <summary>
/// DefectView.xaml 的交互逻辑
/// </summary>
public partial class DefectView : Window
{
    private ObservableCollection<DefectCls> defectClsList;
    private string _savePath = "Products\\shortconfig.json";

    public DefectView()
    {
        InitializeComponent();
        defectClsList = new();

        if (File.Exists(_savePath))
        {
            using (var filestream = new FileStream(_savePath, FileMode.Open, FileAccess.Read))
            {
                defectClsList = new ObservableCollection<DefectCls>(JsonSerializer.Deserialize<List<DefectCls>>(filestream) ?? new());
            }
        }
        DgDefect.ItemsSource = defectClsList;
    }

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        defectClsList.Add(new DefectCls());
    }

    private void Del_Click(object sender, RoutedEventArgs e)
    {
        if (DgDefect.SelectedItem is DefectCls defectcls)
        {
            defectClsList.Remove(defectcls);
        }
    }

    private bool IsSet;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (GlobalData.Instance.LoginPeople != "工程师")
        {
            MessageBox.Show("无操作权限，请切换工程师账号！");
            return;
        }
        IsSet = true;
    }

    private void DgDefect_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (IsSet && DgDefect.SelectedItem is DefectCls defectcls)
        {
            defectcls.ShortKey = e.Key.ToString();
        }
        IsSet = false;
    }

    private void Window_Closed(object sender, System.EventArgs e)
    {
        if (File.Exists(_savePath)) { File.Delete(_savePath); }
        using (var filestream = new FileStream(_savePath, FileMode.CreateNew, FileAccess.Write))
        {
            JsonSerializer.Serialize(filestream, defectClsList, new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
            });
        }
    }
}

public class DefectCls : INotifyPropertyChanged
{
    public int Id { get; set; }
    public int BinCode { get; set; }

    private string _defectName = string.Empty;
    public string DefectName
    {
        get => _defectName; set
        {

            if(_defectName != value)
            { _defectName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DefectName)));
            }
        }
    }
    private string _shortKey = string.Empty;
    public string ShortKey
    { get => _shortKey; set { _shortKey = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShortKey))); } }

    public event PropertyChangedEventHandler? PropertyChanged;
}

public class LotTimes : INotifyPropertyChanged
{
    public int Id { get; set; }
    public int Times { get; set; } = 0;

    private string _lotID = string.Empty;
    public string LotID
    {
        get => _lotID; set
        {

            if (_lotID != value)
            {
                _lotID = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(LotID)));
            }
        }
    }

    public event PropertyChangedEventHandler? PropertyChanged;
}